package com.sl.au.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;

import com.sl.au.entity.*;
import com.sl.au.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.service.AfterSaleMaintainService;
import com.sl.utils.CRM;

@Service("afterSaleMaintainService")
@Transactional
public class AfterSaleMaintainServiceImpl extends BaseServiceImpl implements AfterSaleMaintainService {

	@Autowired
	MaintainSchemeRepository maintainSchemeRepository;
	
	@Autowired
	MaintainItemRepository maintainItemRepository;
	
	@Autowired
	MaintainItemDeviceRepository maintainItemDeviceRepository;

	@Autowired
	ContractRepository contractRepository;

	@Autowired
	DeviceRepository deviceRepository;

	@Autowired
	OptionRepository optionRepository;
	
	@Autowired
	DeviceDetailRepository deviceDetailRepository;
	
	@Override
	public MaintainSchemeRepository getRepository() {
		// TODO Auto-generated method stub
		return this.maintainSchemeRepository;
	}
	@Override
	public ContractRepository getContractRepository(){
		return this.contractRepository;
	}
	@Override
	public MaintainItemRepository getRepository1() {
		// TODO Auto-generated method stub
		return this.maintainItemRepository;
	}
	
	@Override
	public MaintainItemDeviceRepository getRepository2() {
		// TODO Auto-generated method stub
		return this.maintainItemDeviceRepository;
	}

	@Override
	public OptionRepository getOptionRepository() {
		// TODO Auto-generated method stub
		return this.optionRepository;
	}
	
	@Override
	public DeviceDetailRepository getDeviceDetailRepository() {
		// TODO Auto-generated method stub
		return this.deviceDetailRepository;
	}
	
	@Override
	public void initialDevice(String contractId) {
		List<DeviceDetail> deviceDetails = new ArrayList<DeviceDetail>();
		List<QuoteDevice> quoteDevices = getDevices(contractId);
		for (QuoteDevice quoteDevice : quoteDevices){
			String deviceId = quoteDevice.getId();
			int number = quoteDevice.getNumber();
			long count = getDeviceDetailsCount(deviceId);
			if (number>count){
				for(int n=0; n<number-count; n++){
					DeviceDetail deviceDetail = new DeviceDetail();
					deviceDetail.setContractID(quoteDevice.getContractID());
					deviceDetail.setDeviceID(deviceId);
					deviceDetail.setProductName(quoteDevice.getProductName());
					deviceDetail.setProductType(quoteDevice.getProductType());
					deviceDetail.setSubDivideType(quoteDevice.getSubDivideType());
					deviceDetail.setProductModel(quoteDevice.getProductModel());
					deviceDetail.setPower(quoteDevice.getPower());
					deviceDetail.setViscosity(quoteDevice.getViscosity());
					deviceDetail.setFlowMaterial(quoteDevice.getFlowMaterial());
					deviceDetail.setTransMedia(quoteDevice.getTransMedia());
					deviceDetail.setProportion(quoteDevice.getProportion());
					deviceDetail.setEffectiveNPSHA(quoteDevice.getEffectiveNPSHA());
					deviceDetail.setFlux(quoteDevice.getFlux());
					deviceDetail.setTemperature(quoteDevice.getTemperature());
					deviceDetail.setMustMPSHA(quoteDevice.getMustMPSHA());
					deviceDetail.setPumpLift(quoteDevice.getPumpLift());
					deviceDetail.setInPressure(quoteDevice.getInPressure());
					deviceDetail.setUnitPrice(quoteDevice.getUnitPrice());
					deviceDetails.add(deviceDetail);
				}
			}
		}
		this.deviceDetailRepository.save(deviceDetails);
	}
	
	@Override
	public List<QuoteDevice> getDevices(String contractId) {
		return this.maintainSchemeRepository.getDeviceById(contractId);
	}
	
	@Override
	public long getDeviceDetailsCount(String deviceId) {
		return this.maintainSchemeRepository.getDeviceDetailCountById(deviceId);
	}
	
	@Override
	public List<DeviceDetail> getAllDetails(int start, int length, String contractId, String orderName, String dir) {
		Page<DeviceDetail> pages=null;
		Pageable pageable=new PageRequest(start/length,length,Sort.Direction.DESC,"id");
		pages=maintainSchemeRepository.getAllDetailById(contractId, pageable);
		return pages.getContent();
	}
	@Override
	public long getAllDetailsCount(String contractId) {
		return this.maintainSchemeRepository.getAllDetailCountById(contractId);
	}
	
	@Override
	public List<DeviceDetail> getAllPlanDetails(int start, int length, String contractId, String orderName, String dir) {
		Page<DeviceDetail> pages=null;
		Pageable pageable=new PageRequest(start/length,length,Sort.Direction.DESC,"id");
		pages=maintainSchemeRepository.getAllPlanDetailById(contractId, pageable);
		return pages.getContent();
	}
	@Override
	public long getAllPlanDetailsCount(String contractId) {
		return this.maintainSchemeRepository.getAllPlanDetailCountById(contractId);
	}
	
	@Override
	public List<MaintainScheme> getMaintainScheme(int start, int length, String key, String orderName, String dir) {
		Page<MaintainScheme> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		pages =maintainSchemeRepository.findMaintainScheme(key, pageable);
		return pages.getContent();
	}

	/**
	 * 售后计划方案维护弹窗数据
	 * @param start
	 * @param length
	 * @param key
	 * @param orderName
	 * @param dir
	 * @return
	 */
	@Override
	public List<MaintainScheme> getPage(int start, int length, String key, String orderName, String dir) {
		Page<MaintainScheme> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "id");
		pages = maintainSchemeRepository.findAll(pageable);
		return pages.getContent();
	}

	@Override
	public List<MaintainScheme> getAllSchemes(int start, int length, String key1, Object o, Object o1) {
		Page<MaintainScheme> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		pages =maintainSchemeRepository.findAllSchemes(pageable);
		return pages.getContent();
	}

	@Override
	public List<MaintainItemDevice> getMaintainPlans(int start, int length, String key, String orderName,String dir) {
		Page<MaintainItemDevice> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.ASC, "createTime");
		if (StringUtils.hasText(key)) {
			pages =maintainSchemeRepository.findMaintainPlans(key, pageable);
		}else{
			pages =maintainSchemeRepository.findMaintainPlans("nodata", pageable);
		}
		return pages.getContent();
	}

	/**
	 * 通过条件获取合同弹窗
	 * @param start
	 * @param length
	 * @param key
	 * @param o
	 * @param o1
	 * @return
	 */
	@Override
	public List<Contract> getAfterSaleContracts(int start, int length, String key, Object o, Object o1) {
		Page<Contract> pages=null;
		Pageable pageable=new PageRequest(start/length,length,Sort.Direction.DESC,"id");
		String auditState="通过";
		pages=maintainItemRepository.getAfterSaleContracts2(key, auditState,pageable);
		return pages.getContent();
	}
	
	/**
	 * 通过条件获取合同弹窗
	 * @param start
	 * @param length
	 * @param key
	 * @param o
	 * @param o1
	 * @return
	 */
	@Override
	public List<Contract> getPassContracts(int start, int length, String key, Object o, Object o1) {
		Page<Contract> pages=null;
		Pageable pageable=new PageRequest(start/length,length,Sort.Direction.DESC,"id");
		pages=maintainItemRepository.getPassContracts(key, pageable);
		return pages.getContent();
	}

	@Override
	public List<Contract> getAfterSalePage(int start, int length, String key, Object o, Object o1) {
		Page<Contract> pages=null;
		Pageable pageable=new PageRequest(start/length,length,Sort.Direction.DESC,"id");
		String auditState="通过";
		pages=maintainItemRepository.getAfterSaleContracts1(auditState,pageable);
		return pages.getContent();
	}

	@Override
	public List<MaintainScheme> getSchemes(int start, int length, String key1, Object o, Object o1) {
		Page<MaintainScheme> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		pages =maintainSchemeRepository.findSchemes(key1, pageable);
		return pages.getContent();
	}

	@Override
	public List<MaintainItem> getMaintainItem(int start, int length, String key, String orderName, String dir) {
		Page<MaintainItem> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.ASC, "createTime");
		if (StringUtils.hasText(key)) {
			pages =maintainSchemeRepository.findMaintainItem(key, pageable);
		}else{
			pages =maintainSchemeRepository.findMaintainItem("nodata", pageable);
		}
		return pages.getContent();
	}

	@Override
	public List<MaintainItemDevice> getMaintainItemDevice(int start, int length, String key, String orderName,String dir) {
		Page<MaintainItemDevice> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.ASC, "createTime");
		if (StringUtils.hasText(key)) {
			pages =maintainSchemeRepository.findMaintainItemDevice(key, pageable);
		}else{
			pages =maintainSchemeRepository.findMaintainItemDevice("nodata", pageable);
		}
		return pages.getContent();
	}
	/**
	 * 得到树节点数据
	 * @return
	 */
	@Override
	public List<Map<String,Object>> getNodeMap(List<Option> options){
		List<Map<String, Object>> nodeMap = new ArrayList<Map<String, Object>>();
		for (Option o : options) {
			if (null != o){
				Map<String, Object> map = new HashMap<String, Object>();
				List<Option> detailType=new ArrayList<>();
				if(o.getGroup().equals("产品类型")){
					detailType=optionRepository.findByGroup(o.getTitle());
					if(detailType.size()>0) {
						List<Map<String, Object>> childrenMap = new ArrayList<Map<String, Object>>();
						for (Option d : detailType) {
							Map<String, Object> map1 = new HashMap<String, Object>();
							map1.put("name", d.getTitle());
							map1.put("id",d.getId());
							map1.put("parentId", o.getId());
							map1.put("father",o.getTitle());

							childrenMap.add(map1);
						}
						map.put("isParent", true);
						map.put("open",true);
						map.put("children",childrenMap);
					}
					else {
						map.put("isParent", false);
						map.put("open",false);
					}
					map.put("name",o.getTitle());
					map.put("id",o.getId());
					map.put("parentId",0);
					nodeMap.add(map);
				}
			}
		}
		return  nodeMap;
	}
	
}